Skip to content

Fix PetabStrPrinter for non-integer rational exponents#489

Merged
dweindl merged 2 commits into
PEtab-dev:mainfrom
wshlavacek:fix/petabstrprinter-rational-exponent
Jun 22, 2026
Merged

Fix PetabStrPrinter for non-integer rational exponents#489
dweindl merged 2 commits into
PEtab-dev:mainfrom
wshlavacek:fix/petabstrprinter-rational-exponent

Conversation

@wshlavacek

Copy link
Copy Markdown
Contributor

petab_math_str(sympify_petab(x)) is not the identity for square roots and other non-integer rational exponents. For example, petab_math_str(sqrt(a)) returns a ^ 1/2, which sympify_petab re-parses as (a^1)/2 = a/2. This is a silent mangling of the expression.

The cause is that a non-integer Rational exponent, such as 1/2, is a sympy Atom but prints as the multi-token string 1/2, so the not exp.is_Atom guard added in #421 does not parenthesize it. Since ^ binds tighter than /, the unparenthesized a ^ 1/2 then parses as (a^1)/2.

This fix parenthesizes a non-integer rational exponent explicitly, so petab_math_str(sqrt(a)) == "a ^ (1/2)", which re-parses correctly. Integer powers and the non-atomic base/exponent cases from #421 are unchanged.

A non-integer Rational exponent (e.g. a square root, exponent 1/2) is a sympy
Atom but prints as the multi-token "1/2", so PetabStrPrinter emitted
`sqrt(a)` as the unparenthesized `a ^ 1/2`. Since `^` binds tighter than `/`,
that re-parses as `(a^1)/2 = a/2` -- a silent round-trip corruption
(`petab_math_str(sympify_petab(...))` is not the identity for square roots).

The `not exp.is_Atom` guard added in PEtab-dev#421 covers non-atomic exponents but not
this atomic-yet-multi-token case; parenthesize a non-integer rational exponent
explicitly, so `petab_math_str(sqrt(a)) == "a ^ (1/2)"`, which re-parses
correctly. Integer powers and the PEtab-dev#421 cases are unchanged.
@wshlavacek wshlavacek requested a review from a team as a code owner June 20, 2026 18:33
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.29%. Comparing base (17a70e8) to head (b89f351).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #489   +/-   ##
=======================================
  Coverage   75.29%   75.29%           
=======================================
  Files          62       62           
  Lines        6946     6946           
  Branches     1229     1229           
=======================================
  Hits         5230     5230           
  Misses       1245     1245           
  Partials      471      471           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dweindl dweindl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

@dweindl dweindl merged commit 4bbffd0 into PEtab-dev:main Jun 22, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants